<?php

$head_photo_url = empty($model->picture) ? Yii::app()->baseUrl . '/img/user-128.png' : $model->picture;
?>
<div class="row-fluid">
	<div class="span8 offset2">
<?php $form = $this->beginWidget('CActiveForm', array(
	'id'=>'user-form',
	'enableAjaxValidation'=>true,
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>
	<label class="control-label"><?php echo Yii::t('app', 'Your current password'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->passwordField($model, 'password', array('class'=>'span6', 'value'=>'', 'id'=>'change-password')); ?>
	</div>
	<?php echo $form->error($model,'password', array('id'=>'change-password-error')); ?>
	<div class="space"></div>

	<label class="control-label"><?php echo Yii::t('app', 'Enter a new password'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->passwordField($model, 'password_new', array('class'=>'span6', 'value'=>'', 'id'=>'change-password_new')); ?>
	</div>
	<?php echo $form->error($model,'password_new', array('id'=>'change-password_new-error')); ?>
	<div class="space"></div>

	<label class="control-label"><?php echo Yii::t('app', 'Confirm your password'); ?></label>
	<div class="controls controls-row">
		<?php echo $form->passwordField($model, 'password_new_repeat', array('class'=>'span6', 'value'=>'', 'id'=>'change-password_new_repeat')); ?>
	</div>
	<?php echo $form->error($model,'password_new_repeat', array('id'=>'change-password_new_repeat-error')); ?>
	<div class="space"></div>
	<hr />
	<div class="errorMessage"></div>
	<?php echo CHtml::submitButton(BUtils::T('update_lbl'), array('id'=>'change-button', 'class'=>'btn btn-large btn-primary')); ?>
<?php $this->endWidget(); ?>
	</div>
</div>

<script>
$(function() {
	$('#change-button').click(function(){
		$.ajax({
			type:'POST', url:'<?php echo Yii::app()->createUrl('/user/password'); ?>', cache:false,
			dataType:'html', data:jQuery(this).parents("form").serialize(),
			'beforeSend':function(){beforeSubmit("#change");},
			'error':function(resp){errorSubmit(resp, "#change");},
			'success':function(data){afterSubmit(data, "#change");}
		});
		return false;
	});
});

function beforeSubmit(eId) {
	$(eId + '-button').attr('disabled', true);
	$(eId + '-button-loader').show();
	$(eId + '-password-error').html('');
	$(eId + '-password_new-error').html('');
	$(eId + '-password_new_repeat-error').html('');
}
function errorSubmit(res, eId) {
	$(eId + '-button').attr('disabled', false);
	$(eId + '-button-loader').hide();
	var data = JSON.parse(res.responseText);
	for (var prop in data) {
		if (data.hasOwnProperty(prop)){
			$(eId+'-'+prop+'-error').show().html(data[prop][0]);
		}
	}
}
function afterSubmit(data, eId) {
	$(eId + '-button').attr('disabled', false);
	//notify: when merchant choose to the notification option
	$.bootstrapGrowl(data, {type:'success'});
}
</script>